package com.chenjinyan.back.controller;

import com.chenjinyan.back.entity.ConnectionEntity;
import com.chenjinyan.back.service.ConnectionService;
import com.chenjinyan.back.utils.ExceptionEnum;
import com.chenjinyan.back.utils.ExceptionHandle;
import com.chenjinyan.back.utils.ResultUtil;
import com.chenjinyan.back.vo.ConnectionVo;
import com.chenjinyan.back.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/connection")
public class ConnectionController {

    @Autowired
    private ExceptionHandle exceptionHandle;

    @Autowired
    ConnectionService connectionService;

    @PostMapping("/insert")
    public Result getConnectionInfo(@Valid @RequestBody ConnectionVo connectionVo, BindingResult bindingResult) {
        Result result = ResultUtil.success();
        try {
            //参数校验
            if (bindingResult.hasErrors()) {
                List<ObjectError> errorList = bindingResult.getAllErrors();
                for (ObjectError error : errorList) {
                    ExceptionEnum.PARAMETER_WRONG.setMsg(error.getDefaultMessage());
                    return ResultUtil.error(ExceptionEnum.PARAMETER_WRONG);
                }
            }
            result = connectionService.insertConnection(connectionVo);
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @PostMapping("/getConnection")
    public Result getConnection(@RequestBody ConnectionVo connectionVo) {
        Result result = ResultUtil.success();
        try {
            result = connectionService.getConnection(connectionVo);
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }


    @PostMapping("/delConnection")
    public Result delConnection(@RequestBody List<String> ids) {
        Result result = ResultUtil.success();
        try {
            //查找用户
            result = connectionService.delConnection(ids);
            return result;
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }


    @PostMapping("/update")
    public Result updateConnection(@RequestBody ConnectionVo connectionVo) {
        Result result = ResultUtil.success();
        if (null == connectionVo.getId()) {
            result = ResultUtil.error(ExceptionEnum.CONNECTION_ID_IS_NULL);
            return result;
        }
        try {
            List<ConnectionEntity> list = connectionService.getConnectionById(connectionVo);
            if (null == list || list.size() == 0) {
                result = ResultUtil.error(ExceptionEnum.CONNECTION_IS_NULL);
                return result;
            }
            connectionService.updateConnection(connectionVo);
            return result;
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
            return result;
        }
    }


}
